import BasePageCtrl, { AnimType } from "./BasePageCtrl";
import UiManager from "./UiManage";

import n from "./AudioManager"
import l from "./PlayerDataSys"
import s from "./AbTestMgr"
import c from "./OutsideGuideExperimentMgr"
import u from "./PeripheralSystemDataMgr"
import p from "./EventMgr"
import d from "./GameEventType"
import h from "./MatchGameEventType"
import f from "./frameworkManager"
import g from "./AdManager"
import y from "./SdkHelper"
import m from "./EngineUtil"
import _ from "./MatchGameData"
import v from "./BasePageCtrl"
import b from "./PageMgr"
import C, { OutSideLevelGuide } from "./PeripheralSystemMgr"
import T from "./UiManage"
import M from "./MatchGameMoneyPage"

const { ccclass, property } = cc._decorator;

@ccclass
export default class MatchGameMoneyPageCtrl extends BasePageCtrl {
    ui = null;
    outSideGuideStep = null;
    nodeData = [];

    public _animType = AnimType.NONE;
    public _touchControl = false;
    public _hasPeneLock = true;
    public _hasBlack = true;
    public _hasTouchLock = false;
    public _hasBlackTouch = false;

    onLoad() {
        this.onUILoad();

        this.addButtonListen();
        this.addEvent();
        super.onLoad();
    }

    start() {
    }

    onUILoad() {
        this.ui = this.node.addComponent(M);
    }

    __init(e: any) {
        this.node.setContentSize(cc.winSize);
        this.ui.topNodeParent.removeAllChildren();
        this.nodeData = [];
        const topNodeArr = u.getGameShowTopNodeArr();
        const isShowClock = u.isShowClock();
        for (let index = 0; index < topNodeArr.length; index++) {
            const item = topNodeArr[index];
            const type = item.type;
            const topNode = item.topNode;
            topNode.opacity = 0;
            const node = cc.instantiate(topNode);
            node.parent = this.ui.topNodeParent;
            const button = node.getComponent(cc.Button);
            if (node.getComponent(cc.Widget)) {
                node.getComponent(cc.Widget).enabled = false;
            }
            if (node.name === "cashNode") {
                if (button) {
                    button.interactable = true;
                }
                UiManager.addButtonListen(node, this.clickWithDraw, this);
            }
            if (node.name === "fundNode") {
                if (button) {
                    button.interactable = true;
                }
                UiManager.addButtonListen(node, this.clickFundWithDraw, this);
            }
            if (node.name === "btn_day_click") {
                if (button) {
                    button.interactable = true;
                }
                UiManager.addButtonListen(node, this.clickDay, this);
                node.active = isShowClock && topNode.active;
            }
            if (node.name === "topNode") {
                if (button) {
                    button.interactable = true;
                }
                UiManager.addButtonListen(node, this.clickTicket, this);
            }
            this.scheduleOnce(() => {
                node.setPosition(cc.v3(m.getNodeAToNodeBPoint(topNode, node)));
                node.opacity = 255;
                if (index === topNodeArr.length - 1) {
                    this.checkCanWithDraw();
                }
            });
            this.nodeData.push({
                topNode: node,
                type: type
            });
        }
        this.ui.lab_game_level.getComponent(cc.Label).string = `第${e.level}关`;
        this.outSideGuideStep = e.guide_step;
        this.ui.btn_close.active = e.guide_step !== OutSideLevelGuide.BREAKTHROUGH;
        u.setRollingNoticeParent(this.ui.noticeParent);
        this.updateClockShow();
    }

    addButtonListen() {
        UiManager.addButtonListen(this.ui.btn_close, this.clickClose, this);
        UiManager.addButtonListen(this.ui.btn_startGame, this.startGameCallback, this, 2000);
    }

    addEvent() {
        p.listen(d.GAME_START_TOP_CASH, this.updateTopCash, this);
        p.listen(d.GAME_TOP_CLOCK_SHOW, this.updateClockShow, this);
    }

    removeEvent() {
        p.ignore(d.GAME_START_TOP_CASH, this.updateTopCash, this);
        p.ignore(d.GAME_TOP_CLOCK_SHOW, this.updateClockShow, this);
    }

    updateTopCash() {
        let e = this.getTopNodeByType(1);
        if (e) {
            let t = e.getChildByName("cashCount");
            if (t) {
                t.getComponent(cc.Label).string = m.getCashBalance(l.cash_balance) + "";
                this.checkCanWithDraw();
            } else {
                f.log("展示红包数量的节点不存在");
            }
        } else {
            f.log("关卡开始前的顶部节点获取错误请检查");
        }
    }

    getTopNodeByType(e) {
        for (let t = 0; t < this.nodeData.length; t++) {
            let a = this.nodeData[t];
            let o = a.type;
            let i = a.topNode;
            if (o == e) return i;
        }
        return null;
    }

    checkCanWithDraw() {
        let e = u.isCanWithDraw();
        let t = this.getTopNodeByType(1);
        if (t) {
            let a = t.getChildByName("btn_payment");
            let o = t.getChildByName("btn_canPay");
            if (a && a) {
                if (e) {
                    u.checkShowPayMent();
                    a.active = false;
                    o.active = true;
                } else {
                    a.active = true;
                    o.active = false;
                }
            } else {
                f.log("可打款按钮显示错误");
            }
        } else {
            f.log("关卡开始前的顶部节点获取错误请检查");
        }
    }

    startGameCallback() {
        this.checkEndOutsideGuide();
        this.clickClose();
        if (_.full_ad_flag) {
            y.showForceToast("关卡加载中，广告5秒可关闭");
            if (!m.getLocalData("ad_sound_play")) {
                n.getInstance().playMusic("ad_toast", false);
                m.setLocalData("ad_sound_play", "1");
            }
            g.getInstance().playFullVideoAd(function () {
                u.reportVideoBuriedPoint(VideoBuriedPoint.FULL_AD);
                p.trigger(h.LOAD_MATCH_SCENE);
            }, function () {
                p.trigger(h.LOAD_MATCH_SCENE);
            });
        } else {
            p.trigger(h.LOAD_MATCH_SCENE);
        }
    }

    clickClose() {
        for (let e = u.getGameShowTopNodeArr(), t = 0; t < e.length; t++) {
            let a = e[t];
            let o = a.type;
            a.topNode.opacity = 255;
        }
        p.trigger(d.SET_ROLL_NOTICE_PARENT);
        this.hide();
        let i;
        i = s.isOptimizeTravelFundFlag() ? c.OutSideLevelGuideExperimentStep.CONTINUE_GAME : OutSideLevelGuide.CONTINUE_GAME;
        this.outSideGuideStep >= i && p.trigger(d.CLOSE_OUTSIDE_GUIDE, {
            guideShow: true
        });
    }

    checkEndOutsideGuide() {
        let e = u.getOutsideGuideStep();
        let t = _._gameLevel;
        if (s.isOptimizeTravelFundFlag()) {
            if (e == c.OutSideLevelGuideExperimentStep.CASH_WITHDRAW) {
                u.setOutsideStep(c.OutSideLevelGuideExperimentStep.CONTINUE_GAME);
                C.reportOutsideGuideStep(c.OutSideLevelGuideExperimentStep.CONTINUE_GAME);
            }
        } else if (e == OutSideLevelGuide.MOVE_REWARD) {
            u.setOutsideStep(OutSideLevelGuide.CONTINUE_GAME);
            C.reportOutsideGuideStep(OutSideLevelGuide.CONTINUE_GAME);
        }
        e == OutSideLevelGuide.FUND_WITHDRAW && 1 == t && y.reportData("enter_first_level");
    }

    onDestroy() {
    }

    clickWithDraw() {
        C.getExtractInfo();
    }

    clickFundWithDraw() {
        C.getTravelExtractInfo();
    }

    clickDay() {
        C.getClockTaskList();
    }

    updateClockShow() {
        let e = this.getTopNodeByType(2).getChildByName("fundTipNode");
        let t = e.getChildByName("fundTipsLabel");
        let a = u.getCurrentLocation();
        let o = "";
        if (a >= 11) {
            e.active = false;
        } else {
            e.active = true;
            if (a < 5) {
                let i = 8 - a;
                o = "<color=#FFFFFF>预计再到达\n</color><color=#FCFF1B>" + (i > 0 ? i : 0) + "个</color><color=#FFFFFF>城市即可\n</color><color=#FFFFFF>提现</color><color=#FCFF1B>500元</color>";
                t.getComponent(cc.RichText).string = o;
            } else {
                o = "<color=#FFFFFF>再赚</color><color=#FCFF1B>" + m.getCashBalance(5e4 - l.gold_balance) + "元\n</color><color=#FFFFFF>可提现</color><color=#FCFF1B>500元</color>";
                t.getComponent(cc.RichText).string = o;
                this.playAirBubbleAction(e);
            }
        }
    }

    playAirBubbleAction(e) {
        cc.tween(e)
            .to(0.5, { scale: 1.2 })
            .to(0.5, { scale: 1 })
            .call(function () {
                e.getComponent(cc.Animation).play("airbubble");
            })
            .start();
    }

    clickTicket() {
        b.showPage("TravelToastPage", {
            type: 1,
            cityId: null
        });
    }

    static prefabUrl = "MatchGameMoneyPage";
    static className = "MatchGameMoneyPageCtrl";
}
